package br.com.gerenteweb.facesConverter;

import java.io.Serializable;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.gerenteweb.beans.Fornecedor;
import br.com.gerenteweb.daos.FornecedorDao;

@FacesConverter(value = "fornecedorConverter", forClass = Fornecedor.class)
public class FornecedorConverter implements Converter, Serializable {

	@Override
	public String getAsString(FacesContext facesContext,
			UIComponent uiComponent, Object object) {
		Fornecedor fornecedor = new Fornecedor();
		String retorno = "";

		if (object != null) {
			fornecedor = (Fornecedor) object;
			retorno = String.valueOf(fornecedor.getId());
		}
		return retorno;
	}

	@Override
	public Fornecedor getAsObject(FacesContext facesContext,
			UIComponent uiComponent, String value) {
		try {
			return new FornecedorDao().getPorID(Long.parseLong(value));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
